package com.sig;

import com.sig.util.JdbcUtil;

import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;

/**
 * @author sig
 * @since 2016-6-4 16:14:28
 */
public class CdKeyGenerator {

    public static void main(String[] args) {
        JFrame frame = new CdKeyGeneratorFrame();
        frame.setLocation(600, 200);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class CdKeyGeneratorFrame extends JFrame {

    CdKeyGeneratorFrame() {
        setTitle("激活码生成器");

        JPanel northPanel = new JPanel();
        add(northPanel, BorderLayout.NORTH);
        northPanel.setLayout(new GridLayout(0, 2));

        northPanel.add(new JLabel("数据库: ", SwingConstants.TRAILING));
        final JTextField dbField = new JTextField();
        northPanel.add(dbField);

        northPanel.add(new JLabel("用户名: ", SwingConstants.TRAILING));
        final JTextField userField = new JTextField();
        northPanel.add(userField);

        northPanel.add(new JLabel("密码: ", SwingConstants.TRAILING));
        final JTextField pwdField = new JTextField();
        northPanel.add(pwdField);

        northPanel.add(new JLabel("数量: ", SwingConstants.TRAILING));
        final JTextField numField = new JTextField();
        northPanel.add(numField);

        northPanel.add(new JLabel("前缀个数: ", SwingConstants.TRAILING));
        final JTextField prefixNumField = new JTextField();
        northPanel.add(prefixNumField);

        northPanel.add(new JLabel("批次: ", SwingConstants.TRAILING));
        final JTextField batchField = new JTextField();
        northPanel.add(batchField);

        northPanel.add(new JLabel("尾数: ", SwingConstants.TRAILING));
        final JTextField suffixField = new JTextField();
        northPanel.add(suffixField);

        northPanel.add(new JLabel("礼包名称: ", SwingConstants.TRAILING));
        final JTextField nameField = new JTextField();
        northPanel.add(nameField);

        final JTextArea result = new JTextArea(10, 25);
        add(new JScrollPane(result));

        JPanel southPanel = new JPanel();
        add(southPanel, BorderLayout.SOUTH);

        JButton clearBtn = new JButton("清空");
        southPanel.add(clearBtn);
        clearBtn.addActionListener(event -> {
            result.setText("");
        });

        JButton confirmBtn = new JButton("确定");
        southPanel.add(confirmBtn);
        confirmBtn.addActionListener(event -> new SwingWorker<Void, Void>() {

            @Override
            protected Void doInBackground() {

                if (dbField.getText().trim().isEmpty() || userField.getText().trim().isEmpty()
                        || pwdField.getText().trim().isEmpty() || numField.getText().trim().isEmpty()
                        || prefixNumField.getText().trim().isEmpty() || batchField.getText().trim().isEmpty()
                        || suffixField.getText().trim().isEmpty() || nameField.getText().trim().isEmpty()) {
                    result.setText("输入不能为空");
                    return null;
                }
                String db = dbField.getText().trim();
                String user = userField.getText().trim();
                String password = pwdField.getText().trim();
                int num = Integer.valueOf(numField.getText().trim());
                int prefixNum = Integer.valueOf(prefixNumField.getText().trim());
                int batch = Integer.valueOf(batchField.getText().trim());
                int suffix = Integer.valueOf(suffixField.getText().trim());
                String name = nameField.getText().trim();
                JdbcUtil jdbc = new JdbcUtil();

                try {
                    jdbc.getConn(db, user, password);
                    result.setText(jdbc.insert(num, prefixNum, batch, suffix, name) ? "数据添加成功" : "数据添加失败");
                } catch (SQLException | ClassNotFoundException e) {
                    result.setText(e + "");
                }
                return null;
            }
        }.execute());
        pack();
    }
}
